/**
 * <h3>sysPlaInfConfController</h3>
 * <p> SysPlaInfConf</p>
 *
 * <h3>功能描述</h3>
 * <p> 系统平台信息管理</p>
 * <p> 用于都前端请求返回数据</p>
 * <p> </p>
 *
 * <h3>元数据</h3>
 * <ul>
 *    <li> 作者：郑付林</li>
 *    <li> 创建日期：2025/5/6 22:03</li>
 * </ul>
 *
 * <h3>其他信息</h3>
 * <ul>
 *    <li> </a></li>
 *    <li> </li>
 * </ul>
 *
 * @apiNote 该类属于核心模块，需保证线程安全
 * @since 1.0.0
 */
package com.dnndo.web.controller.system;

import com.dnndo.common.annotation.Log;
import com.dnndo.common.core.domain.AjaxResult;
import com.dnndo.common.core.domain.PageResult;
import com.dnndo.common.core.domain.R;
import com.dnndo.common.utils.StringUtils;
import com.dnndo.common.utils.bean.BeanDtoVoUtil;
import com.dnndo.system.domain.SysPlaInfConf;
import com.dnndo.common.enums.BusinessType;
import com.dnndo.system.service.ISysPlaInfConfService;
import com.github.pagehelper.PageInfo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.dnndo.common.utils.PageUtils.startPage;
import static com.dnndo.common.utils.SecurityUtils.getUserId;

@RestController
@RequestMapping("/system/plaInfConf")
public class sysPlaInfConfController {
  private final ISysPlaInfConfService plaInfConfService;

  public sysPlaInfConfController(ISysPlaInfConfService plaInfConfService) {
    this.plaInfConfService = plaInfConfService;
  }

  /**
   * 返回配置列表
   *
   * @param plaInfConf 查询条件
   * @return 查询结果
   */
  @PreAuthorize("@ss.hasPermi('system:SysPlaInfConf:list')")
  @GetMapping("/list")
  public PageResult<SysPlaInfConf> list(SysPlaInfConf plaInfConf,
                                        @RequestParam(defaultValue = "1") int page,
                                        @RequestParam(defaultValue = "10") int size) {
    startPage(page, size); // 显式设置分页参数
    List<SysPlaInfConf> list = plaInfConfService.selectList(plaInfConf);
    System.out.println("list = " + list);
    if (StringUtils.isNotBlank(plaInfConf.getInformation())) { // 使用工具类避免空指针
      list = list.stream()
        .filter(item -> StringUtils.containsIgnoreCase(item.getInformation(), plaInfConf.getInformation()))
        .toList();
    }

    return PageResult.pageOk(BeanDtoVoUtil.listVo(list, SysPlaInfConf.class), new PageInfo<>(list).getTotal());
  }

  /**
   * 删除配置管理
   *
   * @param id 删除对应id的配置数据
   * @return 更新结果
   */
  @PreAuthorize("@ss.hasPermi('system:SysPlaInfConf:del')")
  @Log(title = "删除平台信息配置", businessType = BusinessType.DELETE)
  @DeleteMapping("/delete/{id}")
  public AjaxResult delete(@PathVariable Long id) {
    //参数校验
    if (id == null || id <= 0) {
      return AjaxResult.error("参数错误：无效的网站ID");
    }
    //执行删除
    int rows = plaInfConfService.deleteSysPlaInfConfByWebId(id);
    //如果删除成功，则删除缓存
    if (rows > 0) {
      return AjaxResult.success("更新成功");
    } else {
      return AjaxResult.error("更新失败");
    }
  }

  /**
   * 修改配置管理
   *
   * @param plaInfConf 配置管理对象
   * @return 修改结果
   */
  @PreAuthorize("@ss.hasPermi('system:SysPlaInfConf:edit')")
  @Log(title = "配置管理修改", businessType = BusinessType.UPDATE)
  @PostMapping("/update")
  public AjaxResult edit(@Validated @RequestBody SysPlaInfConf plaInfConf) {
    // 参数非空校验
    if (plaInfConf == null || plaInfConf.getId() == null || plaInfConf.getId() <= 0) {
      //参数错误：网站ID不能为空
      return AjaxResult.error("错误");
    }
    // 设置更新人和更新时间
    plaInfConf.setUpdateBy(getUserId().toString());
    plaInfConf.setUpdateTime(new Date());

    // 调用服务层进行更新操作
    int rows = plaInfConfService.updateSysPlaInfConf(plaInfConf);

    // 返回结果
    if (rows > 0) {
      return AjaxResult.success("更新成功");
    } else {
      return AjaxResult.error("更新失败");
    }

  }

  /**
   * 获取配置详情
   *
   * @param id 获取对应id的配置数据
   * @return 返回配置数据
   */
  @GetMapping("/detail/{id}")
  public R<SysPlaInfConf> getInfo(@PathVariable("id") Long id) {
    SysPlaInfConf plaInfConf = plaInfConfService.selectConfById(id);
    return R.ok(plaInfConf);
  }

  @Log(title = "新增配置", businessType = BusinessType.INSERT)
  @PostMapping("/add")
  public AjaxResult add(@Validated @RequestBody SysPlaInfConf plaInfConf) {
    System.out.println("plaInfConf = " + plaInfConf);

    // 设置更新人和更新时间
    plaInfConf.setUpdateBy(getUserId().toString());
    plaInfConf.setUpdateTime(new Date());

    // 调用服务层进行更新操作
    int rows = plaInfConfService.insertSysPlaInfConf(plaInfConf);

    // 返回结果
    if (rows > 0) {
      return AjaxResult.success("更新成功");
    } else {
      return AjaxResult.error("更新失败");
    }

  }


  //  接口 获取平台配置信息
  @GetMapping("/platform_info")
  public R<Map<String, Map<String, Object>>> platform_info() {
    List<Map<String, Object>> plaInfConf = plaInfConfService.getplatform_info();
    Map<String, Map<String, Object>> resultMap = new HashMap<>();
    for (Map<String, Object> item : plaInfConf) {
      String key = (String) item.get("information");
      resultMap.put(key, item);
    }
    return R.ok(resultMap);
  }
}
